@extends('admin.admin')

@section('content')
<link rel="stylesheet" href="{{ asset('/js/bootstrap-switch/css/bootstrap-switch.min.css') }}">
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        限时折扣管理
        <small>限时折扣列表</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/admin"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li ><a href="/admin/discount">限时折扣管理</a></li>
        <li class="active">限时折扣列表</li>
    </ol>
</section>

<?php

Form::macro(
        'selectRequired',
        function($name, $options = [], $selected = null, $attributes = [], $disabled = [])
        {
            $html = '<select name="' . $name . '"';
            foreach ($attributes as $attribute => $value)
            {
                $html .= ' ' . $attribute . '="' . $value . '"';
            }
            $html .= '>';

            foreach ($options as $value => $text)
            {
                $html .= '<option value="' . $value . '"' .
                        ($value == $selected ? ' selected="selected"' : '') .
                        (in_array($value, $disabled) ? ' disabled="disabled"' : '') . '>' .
                        $text . '</option>';
            }

            $html .= '</select>';

            return $html;
        }
);?>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">

            <div class="box box-primary ">
                <div class="box-body table-responsive">

                    {!! Form::model(@$discounts,array('route' => 'admin.discount.index','method' => 'get','class'=>'form-search form-inline pull-right')) !!}

                        <?php $select = $category::getTreeList();?>

                        <?php

                        function myspace($n,$hasChild){
                            $enum = ['├','└'];
                            if ($hasChild){
                                $space = $enum[0];
                            }else{
                                $space = $enum[1];
                            }
                            $str = '';
                            for ($i=0;$i<=$n;$i++){
                                $str.='&nbsp;&nbsp;';
                            }
                            return $str.$space;
                        }
                        $seletcItem[0] = '所有商品分类';

                        $disabledOption = [];
                        foreach ($select as $k=> $item){

                            if($item->has_children){
                                $disabledOption[]=$item->cat_id;
                            }
                            $seletcItem [$item->cat_id]= myspace($item->deepth,$item->has_children).$item->cat_name;
                        }

                        ?>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::select('discount_type',$discountTypeArr,$search['discount_type'],['class'=>'form-control select2','data-placeholder'=>'所有类型'])!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::text('discount_name',$search['discount_name'],['class'=>'form-control','placeholder'=>'限时折扣名称'])!!}
                            </div>
                            <button type="submit" class="btn btn-primary ">搜索</button>
                            <button type="button" onclick="window.location.href='/admin/discount'" class="btn btn-default">重置</button>

                    {!! Form::close() !!}

                    <a href="{{route('admin.discount.create')}}" class="btn btn-success"><i class="icon  glyphicon glyphicon-plus"></i> 添加</a>

                </div>

            </div><!-- end .box box-primary -->


            <div class="box box-primary">
                <div class="box-header">
                    <h3 class="box-title">限时折扣列表</h3>
                    <div class="box-tools">
                        <a href="{{route('admin.discount.create')}}" type="button" class="btn btn-block btn-success btn-xs"><i class="icon  glyphicon glyphicon-plus"></i> 添加</a>
                    </div>
                </div>
                <!-- /.box-header -->

                <div class="box-body table-responsive no-padding">


                    <table class="table table-hover">
                        <tr>
                            <th>ID</th>
                            <th>名称</th>
                            <th>折扣</th>
                            <!-- <th>用户限用商品数量</th> -->
                            <th>可用类型</th>
                            <th>创建时间</th>
                            <th>结束时间</th>
                            <th>标签</th>
                            {{--<th style="width: 100px">编号</th>--}}

                        </tr>

                        @foreach ($discounts as $item)
                        <tr>
                            <td>{{$item->discount_id}}</td>
                            <td>{{$item->discount_name}}</td>
                            <td>{{$item->discount_value}}</td>
                            <!-- <td>{{$item->limit_amount}}</td> -->
                            <td>
                                <?php
                                    switch ($item->discount_type) {
                                        case '1':
                                            echo '全场通用';
                                            break;
                                        case '2':
                                            echo '商品';
                                            break;
                                        case '3':
                                            echo '分类';
                                            break;
                                        case '4':
                                            echo '店铺';
                                            break;
                                        case '5':
                                            echo '品牌';
                                            break;

                                        default:
                                            echo '类型有误';
                                            break;
                                    }
                                ?>
                            </td>
                            <td>{{$item->created_at}}</td>
                            <td>{{$item->end_time}}</td>
                            <td>{{$item->discount_label}}</td>

                            <td>
                                <a href="{{Route('admin.discount.edit',$item->discount_id)}}?back_url=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" type="button" class="btn  btn-default btn-xs"><i class="icon  glyphicon glyphicon-edit"></i> 修改</a>
                                <button type="button" class="btn  btn-danger btn-xs"  data-toggle="modal" data-target="#modal{{$item->discount_id}}"><i class="icon  glyphicon glyphicon-remove"></i> 删除</button>

                                <div id="modal{{$item->discount_id}}" class="modal fade">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title">删除警告:</h4>
                                            </div>
                                            <div class="modal-body">
                                                <p>确认要删除[{{$item->discount_name}}]吗?</p>
                                            </div>
                                            <div class="modal-footer">


                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                                <a data-id="{{$item->discount_id}}" data-back="<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" class="J-destoryCoupon btn btn-danger">删除</a>


                                            </div>
                                        </div><!-- /.modal-content -->
                                    </div><!-- /.modal-dialog -->
                                </div><!-- /.modal -->
                            </td>
                        </tr>
                        @endforeach

                    </table>

                </div>
                <!-- /.box-body -->

                <div class="box-footer">
                    <!-- 分页 -->
                    <div class="pull-right no-margin-pager">
                        {!! $discounts->appends(['discount_type' => $search['discount_type'], 'discount_name' => $search['discount_name']])->render() !!}
                    </div>

                </div>

            </div>
            <!-- /.box -->

        </div>
    </div>


<!-- Modal -->
<div class="modal fade" id="warmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close close_btn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">信息提示</h4>
      </div>
      <div class="modal-body" id="errorMessage">

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>
</div>

</section>
<!-- /.content -->
<!-- Select2 -->

<script>

$('.J-destoryCoupon').on('mouseup',function(e){

    var that=$(this),discount_id = that.data('id');


    var url = "/admin/discount/"+discount_id;
    var backUrl = that.data('back');
    $.ajax({
        type: "DELETE",
        url: url,
        data: {back_url:backUrl},
        dataType: "json",
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        success: function (msg) {
            //console.log(msg);
            $('#warmModal').modal('show');
            $('#modal'+discount_id).modal('hide');
            if(msg.status){

                $('#errorMessage').html('删除成功');
                location.reload();

            }else{

                $('#errorMessage').html(msg.message);
            }//end if()

        }
    }); //end ajax

});

</script>
@endsection